/*
 * ConfigurationManager.java
 *
 * Created on April 23, 2007, 2:14 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.voidness.twittava.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 *
 * @author void
 */
public class ConfigurationManager {
    
    private Properties props = null;
    private final String propsFile = "twittava.cfg";
    private static ConfigurationManager instance = null;
    
    private ConfigurationManager() {
        if (!new File(propsFile).exists()) {
            try {
                new File(propsFile).createNewFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        props = new Properties();
        InputStream is = null;
        try {
            is = new FileInputStream(new File(propsFile));
            props.load(is);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception exc) {
            }
        }
    }
    
    public void save() {
        OutputStream os = null;
        try {
            os = new FileOutputStream(propsFile);
            props.store(os, "");
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception exc) {
            }
        }
    }
    
    public static ConfigurationManager getInstance() {
        if (instance == null) {
            instance = new ConfigurationManager();
        }
        return instance;
    }
    
    public String getProperty(String key) {
        return props.getProperty(key);
    }
    
    public void setProperty(String key, String value) {
        props.setProperty(key, value);
    }
    
}
